import {defineStore} from "pinia";

export const useConfigStore = defineStore("useConfigStore", () => {
    let storage = localStorage.getItem("CONFIG");
    const config = {
        lang: "zh",
        theme: "light",
    }
    if (storage) {
        let data = JSON.parse(storage)
        if (Object.keys(data).length > 0 && data.constructor === Object){
            Object.keys(data).forEach(key => {
                config[key] = data[key]
            })
        }
    }

    function updateConfig(key, value) {
        localStorage.setItem("CONFIG", JSON.stringify({...config, [key]: value}))
        config[key] = value;
    }

    function getConfig(key) {
        return config[key];
    }

    return {
        updateConfig,
        getConfig
    }
}, true)